package com.manager.controller;

import com.manager.bean.Visitor;
import com.manager.service.VisitorService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

import com.manager.bean.PageInfo;

/**
 * (Visitor)表控制层
 *
 * @author makejava
 * @since 2021-03-02 20:15:30
 */
@Controller
public class VisitorController {
    /**
     * 服务对象
     */
    @Resource
    private VisitorService visitorService;

    @RequestMapping(value = "/findVisitor")
    public String findVisitor(String name, String phone , Integer pageIndex,
                              Integer pageSize, Model model) {

        PageInfo<Visitor> pi = visitorService.findPageInfo(name,phone,
                pageIndex,pageSize);
        model.addAttribute("pi",pi);
        model.addAttribute("name",name);
        return "visitor_list";
    }


    @RequestMapping(value = "/exportvisitorlist", method = RequestMethod.POST)
    @ResponseBody
    public List<Visitor> exportVisitor() {
        List<Visitor> visitorList = visitorService.getAll();
        return visitorList;
    }


    @RequestMapping("/deleteVisitor")
    @ResponseBody
    public String deleteVisitor(Integer Id) {
        boolean s = visitorService.deleteById(Id);
        return "visitor_list";
    }


    @RequestMapping(value = "/addVisitor", method = RequestMethod.POST)
    @ResponseBody
    public String addVisitor(@RequestBody Visitor visitor) {
        visitor.setEnter(new Date(new Date().getTime()));
        Visitor s = visitorService.insert(visitor);
        return "visitor_list";
    }


    @RequestMapping(value = "/updateVisitor", method = RequestMethod.POST)
    public String updateVisitor(Visitor visitor) {
        visitor.setGetout(new Date(new Date().getTime()));
        Visitor s = visitorService.update(visitor);
        return "redirect:/findVisitor";
    }


    @RequestMapping("/findVisitorById")
    public String findVisitorById(Integer Id, HttpSession session, Model model) {
        Visitor s = visitorService.queryById(Id);
        session.setAttribute("s", s);
        model.addAttribute("s", s);
        return "visitor_edit";
    }

}
